package javanlp;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.io.FileUtils;

public class FilesInFolder  {
    public static void main(String[] args) 
            throws IOException, SQLException, Exception{
        int count=1;
        Class.forName("org.postgresql.Driver"); 
        String url = "jdbc:postgresql://localhost/postgres";
        String user = "postgres", passwd = "cinvprobit";
        Connection con = DriverManager.getConnection(url,user,passwd);
 
        String stm = "INSERT INTO java2(id, text, filename) VALUES(?,?,?)";
        PreparedStatement pst = con.prepareStatement(stm);        
        
        String fld = "C:/Users/lenovolap/Dropbox/text4postgres";
        File f = new File(fld);
        File[] files = f.listFiles();
        for (File file : files) {
            //System.out.println(file.getName());
            String fileString = FileUtils.readFileToString(file); 
            //System.out.println(fileString);
            pst.setInt(1, count);
            pst.setString(2, fileString);  
            pst.setString(3, file.getName());
            pst.executeUpdate();                  
            count++;    
          }
        count--;
        System.out.println("\n" + count + 
                " files were successfully inserted into the database.\n");
        if (con != null) {    con.close();      }
    }
}